package com.jacobson.huobi.utils

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.jacobson.core.widget.storage.GlobalStorage
import com.jacobson.huobi.Constants
import com.jacobson.huobi.entities.vo.LoginRespVo

/**
 * 令牌缓存工具类
 */
object TokenStorageUtil {

    /**
     * 保存  缓存与sd卡中
     */
    fun save(info: String) {
//        SDCardUtil.writeSDCard(Base64Util.EncoderStr(info))
        SettingProperties.setUserTokenInfo(Base64Util.EncoderStr(info))
        GlobalStorage.put(Constants.KEY_LOGIN_INFO, Base64Util.EncoderStr(info))
    }

    /**
     * 读取 先读手机缓存,没有再读sd卡
     */
    fun read(): LoginRespVo? {
        var info = GlobalStorage.get(Constants.KEY_LOGIN_INFO) as String?
        if (info.isNullOrEmpty()) {
            info = SettingProperties.getUserTokenInfo()
        }
        if (info.isNullOrEmpty()) {
            return null
        }
        return JSON.parseObject(Base64Util.DecoderStr(info), LoginRespVo::class.java)
    }

    /**
     * 根据key读取数据
     */
    fun readDataByKey(key: String): String? {
        var info = GlobalStorage.get(Constants.KEY_LOGIN_INFO) as String?
        if (info.isNullOrEmpty()) {
            info = SettingProperties.getUserTokenInfo()
        }
        if (info.isNullOrEmpty()) {
            return ""
        }
        var value = JSON.parseObject(Base64Util.DecoderStr(info)).getString(key)
        if (value.isNullOrEmpty()) {
            return ""
        }

        return value
    }

    /**
     * 读取json格式数据
     */
    fun readJsonData(): JSONObject? {
        var info = GlobalStorage.get(Constants.KEY_LOGIN_INFO) as String?
        if (info.isNullOrEmpty()) {
            info = SettingProperties.getUserTokenInfo()
        }
        return JSON.parseObject(Base64Util.DecoderStr(info))
    }
}